﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Diagnostics;


namespace CustomActionTest
{
    [RunInstaller(true)]
    public partial class Installer1 : System.Configuration.Install.Installer
    {
        public Installer1()
        {
            InitializeComponent();
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
            string installationFolder = System.IO.Path.GetDirectoryName(Context.Parameters["assemblypath"].ToString());
            using (Process registryInput = Process.Start(
                System.IO.Path.Combine(installationFolder, "DBUpdater.v11.2.exe"), 
                System.IO.Path.Combine(installationFolder, "DBUpdater.v11.2.exe.config")))
            {
                registryInput.WaitForExit();
                int exitCode = registryInput.ExitCode;
                System.Windows.Forms.MessageBox.Show(exitCode.ToString());
                if (exitCode != 1 && exitCode != 2)
                {
                    throw new InstallException("Some error message here");
                }
            }
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
        }
    }
}
